package com.example.votemanage.Interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
public class TestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
        System.out.println(" 开始拦截 ");
        log.debug("log开始拦截--");
        String requestURI = request.getRequestURI();
        // 允许直接访问 /index
        if (requestURI.equals(request.getContextPath() + "/login")) {
            return true;
        }
        if (requestURI.equals(request.getContextPath() + "/register")) {
            return true;
        }
        if(request.getSession().getAttribute("user")==null){
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView
    ) throws Exception{
        System.out.println(" 开始拦截 ");
        HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,Object handler,Exception ex) throws Exception{
        System.out.println(" 开始拦截 ");
        HandlerInterceptor.super.afterCompletion(request,response,handler,ex);
    }
}
